package com.lhkj.ct.admin.modules.gauge.scale.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.annntation.PreventDuplicateSubmit;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScale;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblScaleSet;
import com.lhkj.ct.meta.modules.gauge.service.ScaleConfigService;
import com.lhkj.ct.meta.modules.gauge.service.ScaleSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 测验组套 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-11-01
 */
@RestController
@RequestMapping("api/plat/gauge/set")
@Api(tags = {"6.2测验组套(量表组合)"})
@Validated
public class ScaleSetController {

    @Resource
    private ScaleSetService scaleSetService;

    @Resource
    private ScaleConfigService scaleConfigService;

    @GetMapping(value = "list")
    @ApiOperation(value = "列表接口")
    //@RequiresPermissions(value = {"system:scaleSet:list"}, logical = Logical.OR)
    public ActionResult<IPage<TblScaleSet>> list(TblScaleSet scaleSet, PageQuery pageQuery) {
        return ActionResult.success(scaleSetService.selectPages(scaleSet, pageQuery));
    }

    @ApiOperation(value = "量表列表", notes = "按量表分类进行分组")
    @GetMapping("scales")
    //@RequiresPermissions(value = {"system:scaleSet:insert", "system:scaleSet:update"}, logical = Logical.OR)
    public ActionResult<List<Map<String, List<Map<String, Object>>>>> groupByCategory(TblScale scale) {
        return ActionResult.success(scaleConfigService.groupByCategory(scale));
    }


    @ApiOperation(value = "表单详情")
    @GetMapping("{cId}/get")
    //@RequiresPermissions(value = {"system:scaleSet:get", "system:scaleSet:update"}, logical = Logical.OR)
    public ActionResult<TblScaleSet> getForm(@PathVariable String cId) {
        return ActionResult.success(scaleSetService.getForm(cId));
    }

    @PreventDuplicateSubmit
    @PostMapping(value = "insert")
    @ApiOperation(value = "新增")
    @Log(title = "测验组套", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = {"system:scaleSet:insert"})
    public ActionResult<TblScale> insert(@Validated @RequestBody TblScaleSet entity) {
        scaleSetService.insert(entity);
        return ActionResult.success();
    }

    @PutMapping(value = "update")
    @ApiOperation(value = "修改")
    @Log(title = "测验组套", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:scaleSet:update"})
    public ActionResult<Void> update(@Validated @RequestBody TblScaleSet entity) {
        return ActionResult.judge(scaleSetService.updateById(entity));
    }

    @DeleteMapping("delete")
    @ApiOperation(value = "删除", notes = "支持批量删除")
    @Log(title = "测验组套", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = {"system:scaleSet:delete"})
    public ActionResult<Void> delete(@RequestBody @NotEmpty(message = "删除的编号不能为空") List<String> ids) {
        int count = scaleSetService.removeByIds(ids);
        return ActionResult.success(String.format("操作成功，共删除%d条记录", count));
    }

    @GetMapping(value = "{cId}/qrCode")
    @ApiOperation(value = "二维码接口")
    public ActionResult<String> getQrCode(@PathVariable String cId) {
        return ActionResult.success("操作成功",scaleSetService.qrCode(cId));
    }
}
